float_eq
Compare IEEE floating point primitives, structs and collections for equality.
This crate provides an API with a focus on making the choices of comparison algorithm(s) and tolerances intuitive to implementers and maintainers, and of providing clear output for debugging and development iteration.
This readme is a quick tour of the crate. For introductory material, guides and discussion see the float_eq guide.
Usage
Add this to your cargo.toml:
[dependencies]
float_eq = "1"
And, if you're using the 2015 edition, this to your crate root:
extern crate float_eq;
Then, you can import items with use
:
use ;
Comparisons
This crate provides boolean comparison operations:
if
And asserts:
const RECIP_REL_TOL: f32 = 0.000_366_210_94;
assert_float_eq!;
Using absolute tolerance, relative tolerance or ULPs based comparison algorithms.
Composite types
Composite types may implement the provided extension traits to be compared on a field-by-field basis:
let a = Complex32 ;
let b = Complex32 ;
assert_float_eq!;
...and if they are homogeneous, with a uniformly applied tolerance across all fields:
assert_float_eq!;
Arrays of any size are supported:
let a = ;
let b = ;
assert_float_eq!;
assert_float_eq!;
As are tuples up to size 12 (inclusive):
let a = ;
let b = ;
assert_float_eq!;
Many standard and core types like Vec
are supported:
let a = vec!;
let b = vec!;
assert_float_eq!;
assert_float_eq!;
There are blanket trait impls for comparing mutable and immutable reference
types, the contents of Cell
, RefCell
, Rc
, Arc
and Box
instances, as
well as for slices, Option
, Vec
, VecDeque
, LinkedList
, BTreeMap
and
HashMap
.
Derivable
The extension traits may be derived for non-generic structs and tuple structs:
let a = Point ;
let c = Point ;
assert_float_eq!;
assert_float_eq!;
Error messages
Asserts provide additional useful context information. For example:
assert_float_eq!;
Panics with this error message:
thread 'main' panicked at 'assertion failed: `float_eq!(left, right, rmax <= t)`
left: `4.0`,
right: `4.000008`,
abs_diff: `0.000008106232`,
ulps_diff: `Some(17)`,
[rmax] t: `0.000004000008`', assert_failure.rs:15:5
Where [rmax] t
shows the tolerance value that the absolute difference was
compared against after being appropriately scaled.
Optional features
This crate can be used without the standard library (#![no_std]
) by disabling
the default std
feature. Use this in Cargo.toml
:
[dependencies.float_eq]
version = "1"
default-features = false
Other optional features:
- derive — provides custom derive macros for all traits.
- num — blanket trait impls for
num::Complex
where it is instanced with a compatible type.
Related efforts
The approx
, float-cmp
, assert_float_eq
and is_close
crates provide
similar floating point comparison capabilities to float_eq
. The almost
crate
divides its API into comparison of floats against zero and non-zero values. The
efloat
crate provides an f32
equivalent type that tracks the maximum
possible error bounds that may have occured due to rounding.
The ieee754
crate is not a comparison library, but provides useful
functionality for decomposing floats into their component parts, iterating over
representable values and working with ULPs directly, amoung other things.
Contributing
Constructive feedback, suggestions and contributions welcomed, please open an issue.
Changelog
Release information is available in CHANGELOG.md.